var Datastore = require('nedb')

function GroupDB(filename) {
    this.db = new Datastore({filename:filename});
    this.db.loadDatabase();
}

GroupDB.prototype.addGroup = function(group) {
    this.db.update({id:group.id}, group, {upsert:true}, function(err, numReplaced, upsert) {
        console.log("err:", err, "num replaced:", numReplaced, "upsert:", upsert);
    });
}
GroupDB.prototype.removeGroup = function(gid) {
        this.db.remove({id:gid},(err,ret)=>{
        // console.log("ret删除群聊+++"+ret)
        // resolve(ret)
        })
}
GroupDB.prototype.findGroup = function(gid) {
    var self = this;
    return new Promise(function(resolve, reject) {
        self.db.findOne({id:gid}).exec(function(err, group) {
            if (err) {
                reject(err);
            } else {
                resolve(group);
            }
        });
    });
}

module.exports = GroupDB;
